package one.stand.controller;

import com.jmp.base.ArticleApi;
import com.jmp.base.dto.ArticlePositionSaveRequest;
import com.jmp.base.dto.ArticleSaveRequest;
import com.jmp.base.dto.ArticleStatusSaveRequest;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ArticleWithBLOBs;
import one.stand.model.ResultModel;
import one.stand.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hujun
 */
@Api(description = "头条")
@RestController
@Slf4j
public class ArticleController implements ArticleApi {
    @Autowired
    private ArticleService articleService;

    @Override
    public ResultModel articleSave(@RequestBody ArticleSaveRequest request) {
        ArticleWithBLOBs article = new ArticleWithBLOBs();
        article.setId(request.getId());
        article.setTemplateType(request.getTemplateType());
        article.setTitle(request.getTitle());
        article.setSubtitle(request.getSubtitle());
        article.setShareCard(request.getShareCard());
        article.setContent(request.getContent());
        article.setAdContent(request.getAdContent());
        article.setTypeId(request.getTypeId());
        article.setOriginName(request.getOriginName());
        article.setOriginUrl(request.getOriginUrl());
        article.setCompanyId(request.getCompanyId());
        articleService.save(article);
        return ResultModel.success();
    }

    @Override
    public ResultModel articleStatusSave(@RequestBody ArticleStatusSaveRequest request) {
        String labels = StringUtils.join(request.getLabels(), ",");
        articleService.updateStatus(request.getId(), request.getStatus(), labels, request.getPosition(), request.getIsPush());
        return ResultModel.success();
    }

    @Override
    public ResultModel articlePositionSave(@RequestBody ArticlePositionSaveRequest request) {
        articleService.updatePosition(request.getId(), request.getPosition());
        return ResultModel.success();
    }
}
